package newtest;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.StatusLine;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;

import javax.net.ssl.SSLContext;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;

/**
 * 根据当前登录人，获取其所属保险公司的自定义参数信息
 */
public class InsurerParamsList {
	
	static String username = "13312344321@163.COM";//对接人员账号
	static String password = "7a9e307df1cb7ae2a503897dbbfcc6f8";//对接人员密码

	static String ip = "127.0.0.1";
	static int port = 8081;
	static String httpProtocol = "http";
	static HttpHost target = null;

	public static void main(String[] args) throws Exception{
		getInsurerParamsList();
	}

	private static void getInsurerParamsList() throws Exception {
		CloseableHttpClient httpclient = createSSLClientDefault();
		HttpClientContext localContext = HttpClientContext.create();
		//busiType:1-寿险产品;2-财险产品;3-其他产品
		HttpGet httpGet = new HttpGet(httpProtocol+"://"+ip+":"+port+"/join/insurerParamsList?busiType=1");
		httpGet.setHeader("Content-Type", "application/json");
		CloseableHttpResponse response = httpclient.execute(target, httpGet, localContext);
		System.out.println("------------------insurerParamsList----------------------");
		StatusLine tLine = response.getStatusLine();
		System.out.println(tLine);
		HttpEntity entity = response.getEntity();
		String data = IOUtils.toString(entity.getContent(), "UTF-8");		
		System.out.println(data);
	}
	
	/**
	 * 获取受信任httpClient
	 * 
	 * @return
	 */
	public static CloseableHttpClient createSSLClientDefault() {
		if (target == null)
			target = new HttpHost(ip, port, httpProtocol);

		CloseableHttpClient httpClient = null;
		CredentialsProvider credsProvider = new BasicCredentialsProvider();
		credsProvider.setCredentials(new AuthScope(target.getHostName(), target.getPort()),
				new UsernamePasswordCredentials(username, DigestUtils.md5Hex(password)));
		SSLContext sslContext;
		try {
			sslContext = new SSLContextBuilder().loadTrustMaterial(null, (certificate, authType) -> true).build();
			SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new String[] { "TLSv1.2" },
					null, new NoopHostnameVerifier());
			httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).setDefaultCredentialsProvider(credsProvider)
					.build();
			return httpClient;
		} catch (KeyManagementException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (KeyStoreException e) {
			e.printStackTrace();
		}
		return null;
	}
}
